﻿using UnityEngine;
using System.Collections;

public class CharactorController : MonoBehaviour {

    public float W_speed = 5f;
    public float S_speed = 3f;
    public float L_speed = 3f;
    public float R_speed = 3f;

    public Transform main_camera;
    public Vector3 delta_postion = new Vector3(2f, 2f, 5f);

	// Use this for initialization
	void Start () {
        main_camera.Rotate(new Vector3(0, 90, 0));
	}
	
	// Update is called once per frame
	void Update () {
        //go ahead
        bool is_run = false;
        if (Input.GetKey(KeyCode.W))
        {
            this.transform.position += Vector3.forward * this.W_speed * Time.deltaTime;
            SwapAnimation("Run");
            is_run = true;
        }

        //towards the back
        if (Input.GetKey(KeyCode.S))
        {
            this.transform.position += Vector3.back * this.S_speed * Time.deltaTime;
            SwapAnimation("Run");
            is_run = true;
        }

        //left
        if (Input.GetKey(KeyCode.A))
        {
            this.transform.position += Vector3.left * this.S_speed * Time.deltaTime;
            SwapAnimation("Run");
            is_run = true;
        }

        //right
        if (Input.GetKey(KeyCode.D))
        {
            this.transform.position += Vector3.right * this.S_speed * Time.deltaTime;
            SwapAnimation("Run");
            is_run = true;
        }
        if (is_run == false)
        {
            SwapAnimation("Idle");
        }
	}

    void SwapAnimation(string name)
    {
        if (!animation.IsPlaying(name))
        {
            animation.CrossFade(name);
            animation.wrapMode = WrapMode.Loop;
        }
    }

    //use this function to control the main camera's position;
    void FixedUpdate()
    {
       main_camera.position = Vector3.Lerp(main_camera.position, transform.position + delta_postion, 0.05f);
      // main_camera.LookAt(transform);
    }
}
